package com.beijiteshop.shop.ui.notification.fragment

import android.view.View
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentNotificationBinding
import com.beijiteshop.shop.model.entity.NotificationEntity
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.notification.adapter.NotificationAdapter
import com.beijiteshop.shop.ui.notification.vm.NotificationVM
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * 消息列表
 */
class NotificationFragment : BaseContainerFragment(R.layout.fragment_notification) {

    private val mBinding: FragmentNotificationBinding by bindView()
    private val mNotifyAdapter: NotificationAdapter  = NotificationAdapter(mutableListOf())
    private val mNotificationVM: NotificationVM by viewModel()
    private val mDefaultNotification: NotificationEntity by lazy {
        NotificationEntity(
            resources.getString(R.string.system_notification),
            resources.getString(R.string.order_message),
            ObservableField(resources.getString(R.string.no_news)),
            ObservableInt(0)
        )
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            NotificationFragment()
    }

    override fun initView(view: View) {
        initRecyclerView()
    }

    override fun initData() {

    }

    override fun onLazyLoad() {
        mNotifyAdapter.setNewInstance(mutableListOf(mDefaultNotification))
        getNotificationCount()
    }

    override fun initEvent() {
        startObserve()

        EventUtil.observe<Boolean>(this,EventKey.DECREASE_MSG_COUNT){readAll->
            if (readAll){
                mDefaultNotification.num.set(0)
                mDefaultNotification.state.set(resources.getString(R.string.no_news))
            }else{
                val count = mDefaultNotification.num.get()-1
                if (count > 0){
                    mDefaultNotification.num.set(count)
                }else{
                    mDefaultNotification.num.set(0)
                    mDefaultNotification.state.set(resources.getString(R.string.no_news))
                }
            }
        }
    }

    private fun initRecyclerView(){
        mNotifyAdapter.setOnItemClickListener { _, _, position ->
            startActivity(PageIndex.NOTIFICATION_DETAIL)
        }

        mBinding.notificationRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mNotifyAdapter
        }
    }

    private fun getNotificationCount(){
        mNotificationVM.getNotificationCount()
    }

    private fun startObserve(){
        mNotificationVM.apply {
            liveNotificationCount.observeKt {count ->
                count?.let {
                    mDefaultNotification.num.set(count.count)
                    var time = count.time
                    if (time.isNullOrEmpty()){
                        time = resources.getString(R.string.no_news)
                    }
                    mDefaultNotification.state.set(time)
                }
            }
        }
    }
}